<?php

namespace App\Http\Controllers\Home\Goods;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    //展示商品详情页面
    public function index($id,$attr,$nowPage = 1,$pageSize = 5)
    {
        //取出5件新品
        $new = DB::table('goods')
                -> where('is_new',1)
                -> limit(5)
                -> orderBy('sort_num')
                -> get();
        //取出5条热销产品
        $hot = DB::table('goods')
                -> where('is_hot',1)
                -> limit(5)
                -> orderBy('sort_num')
                -> get();
    	//根据系列取出所有系列
    	$data = DB::table('goods')
    			-> where('goods_name','like','%' . $attr . '%' )
                -> where('is_on_sale',1)
    			-> get();
        //获取当前商品的信息
    	$info = DB::table('goods') -> where('id',$id) -> first();
    	//根据商品id取出商品属性
    	$attr = DB::table('goods_attr as c1')
    			-> select('c1.*','c2.attr_name','c2.attr_type')
    			-> leftJoin('attribute as c2','c1.attr_id','=','c2.id')
    			-> where('c1.goods_id',$id)
    			-> get();

        //给分页做准备

        //取出商品的评论
        $comment = DB::table('comment') -> where('goods_id',$id) -> get();
        // dd($comment->render());
        //总的评论数目
        $cou = count($comment);
        //计算总的页数
        // $pageCount = ceil($cou / $pageSize);
        // //计算limit的第一个参数
        // $offset = ($nowPage - 1) * $pageCount;

        // //取数据
        // $dataComment = DB::table('comment') 
        //                 -> where('goods_id',$id)
        //                 ->orderBy('id','DESC')
        //                 ->offset($offset)
        //                 -> limit($pageSize) 
        //                 -> get();
        // dd($dataComment);
        //计算好评率
        $goodcom = DB::table('comment') -> where('goods_id',$id) -> where('score','>',3) -> get();
        $goodcou = count($goodcom);
        if($cou != 0){
             $goodslv = ceil(($goodcou / $cou) * 100);
         }else
         {
            $goodslv = 0;
         }
        //取出1级分类
        $cateone = DB::table('category')
                -> where('pid',0)
                -> get();
        //取出二级分类
        $catetwo = array();

        foreach($cateone as $v)
        {
            $catetwo[] = DB::table('category')
                        -> where('pid',$v -> id)
                        -> get();
        }

        // dd($catetwo);
        //取出三级分类
        $catethree = array();
        foreach( $catetwo as $k => $v)
        {
            foreach($v as $k1 => $v1)
            {
                $catethree[] = DB::table('category')
                            -> where('pid', $v1 -> id)
                            -> get();
            }
        }
    	return view('home/goods/detail',['info' => $info,'data' => $data,'attr' => $attr,'hot' => $hot,'new' => $new,'comment' => $comment,'cou' => $cou,'goodslv' => $goodslv,'cateone' => $cateone,'catetwo' => $catetwo,'catethree' => $catethree]);
    }
}
